package com.zy.fee.roadpark.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.model.M_NpNodeRecord;
import com.zygk.library.adapter.BaseListAdapter;
import java.util.ArrayList;
import java.util.List;

public class LeaveApplyAdapter extends BaseListAdapter<M_NpNodeRecord> {
  private List<M_NpNodeRecord> mDatas = new ArrayList<>();

  private ItemDeleteClickListener itemDeleteClickListener;

  public interface ItemDeleteClickListener {
    void onItemDeleteClick(M_NpNodeRecord m, int position);
  }

  public void setItemDeleteClickListener(ItemDeleteClickListener itemDeleteClickListener) {
    this.itemDeleteClickListener = itemDeleteClickListener;
  }

  public LeaveApplyAdapter(Context context, List<M_NpNodeRecord> list) {
    super(context, list);
  }

  public void setData(List<M_NpNodeRecord> list, boolean isLoadMore) {
    if (!isLoadMore) {
      clearAll();
    }
    addALL(list);
    notifyDataSetChanged();
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.item_leave_apply, null, false);
      holder = new ViewHolder(convertView);

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    mDatas = getData();
    final M_NpNodeRecord m = mDatas.get(position);
    holder.tvPlateNumber.setText(m.getCarNumber());
    holder.tvApplyTime.setText(m.getApplyTime());
    holder.tvHandleTime.setText(m.getHandleTime());
    if (m.getApplyState() == 3) {
      holder.tvApplyState.setText("已驶离");
      holder.tvApplyState.setTextColor(mContext.getResources().getColor(R.color.theme_color));
    } else {
      holder.tvApplyState.setText("查看取证");
      holder.tvApplyState.setTextColor(mContext.getResources().getColor(R.color.theme_blue));
    }

    holder.llItem.setOnClickListener(v -> {
      if (itemDeleteClickListener != null) {
        itemDeleteClickListener.onItemDeleteClick(m, position);
      }
    });

    return convertView;
  }

  static class ViewHolder {
    @BindView(R.id.ll_item) LinearLayout llItem;
    @BindView(R.id.tv_plate_number) TextView tvPlateNumber;
    @BindView(R.id.tv_apply_time) TextView tvApplyTime;
    @BindView(R.id.tv_apply_state) TextView tvApplyState;
    @BindView(R.id.tv_handle_time) TextView tvHandleTime;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
